﻿Ext.define("PSI.Knowledge.KnowledgeMain", {

    config: {
        kid: ""
    },

    constructor: function (config) {
        this.initConfig(config);
    },

    createUI: function () {
        var me = this;
        var panel = Ext.create("Ext.panel.Panel", {
            layout: "border",
            items: [
                {
                    xtype: "panel",
                    title: "文章类别",
                    width:"30%",
                    region: "west",
                    layout: "fit",
                    items: [
                        this.getKnowTree()
                    ]

                }, {
                    xtype: "panel",
                    region: "center",
                    width: "70%",
                    layout: "anchor",
                    items: [
                             {
                                 xtype: 'panel',
                                 id:'content',
                                 html: '',
                                 anchor: '100% 60%',
                                 dockedItems: [{
                                     xtype: 'toolbar',
                                     dock: 'bottom',
                                     height:30,
                                     items: [
                                         { xtype: 'label', text: '下载附件:', style: {fontSize:"12px",color:"red"} },
                                         {
                                             xtype:'button',
                                             text: '附件1.xls',
                                             id: "f1",
                                             style:{display:"none"},
                                             margin: '0 0 0 30',
                                             listeners:{
                                                 click: function (me, v) {
                                                     tree = Ext.getCmp("tree");
                                                     var item = tree.getSelectionModel().getSelection();
                                                     themeid = item[0].raw.id
                                                     window.location.href = "/Knowledge/DownLoad?num=1&id=" + themeid
                                                 }
                                                 
                                             }
                                               
                                         }, {
                                             xtype: 'button',
                                             text: '附件2.xls',
                                             id: "f2",
                                             style: { display: "none" },
                                             margin: '0 0 0 30',
                                             listeners: {
                                                 click: function (me, v) {
                                                     tree = Ext.getCmp("tree");
                                                     var item = tree.getSelectionModel().getSelection();
                                                     themeid = item[0].raw.id
                                                     window.location.href = "/Knowledge/DownLoad?num=2&id=" + themeid
                                                 }

                                             }

                                         }
                                     ]
                                 }]
                             }  , {
                                 xtype: 'panel',
                                 tbar: [{
                                     text: '回复信息',
                                     iconCls: "psi-button-add",
                                     handler: function () {
                                         tree = Ext.getCmp("tree");
                                         var item = tree.getSelectionModel().getSelection();
                                         if (item == null || item.length != 1 || item[0].raw.expanded) {
                                             PSI.MsgBox.showInfo("请选择要回复的文章！");
                                             return;
                                         }
                                         me.replywindow(item[0].raw.id);
                                     }
                                 }],
                                 anchor: '100% 40%',
                                 border:0,
                                 layout:'fit',
                                 items: [
                                    this.getReplyItem()
                                 ]
                             }                                                 
                    ]
                }
            ]

        })
        this.panel = panel;
        Ext.getCmp("f2").setVisible(false);
        Ext.getCmp("f1").setVisible(false);
    },

    getMainPanel: function () {
        return this.panel;
    },

    getKnowTree: function () {
        var me = this;
        Ext.define("PSIOrgModel", {
            extend: "Ext.data.Model",
            fields: ["id", "text", "Contents", "expandable", "FileOne", "FileTwo", "expanded", "children", "leaf"]
        });

        var orgStore = Ext.create("Ext.data.TreeStore", {
            storeId: 'zzjg',
            model: "PSIOrgModel",
            proxy: {
                actionMethods: { read: 'POST' },
                type: "ajax",
                url: "/Knowledge/GetTress"
            },
            autoLoad: true,
            listeners: {
                load: function (mes, records, successful, eOpts) {
                    var cmm = me.getKid();
                    if (me.getKid() != null && me.getKid() != "") {

                        var stores = Ext.data.StoreManager.lookup('zzjg');

                        var racond = stores.getNodeById(cmm);

                        var tree = Ext.getCmp("tree");

                        tree.getSelectionModel().select(racond);
                    }
                }
            }
        });

     var orgTree = Ext.create("Ext.tree.Panel", {
            id: 'tree',
            store: orgStore,
            rootVisible: false,
            useArrows: true,
            columns: {
                defaults: {
                    flex: 1,
                    sortable: false,
                    menuDisabled: true,
                    draggable: false
                },
                items: [
                    {
                        dataIndex: "id",
                        hidden: true
                    },
                    {
                        xtype: "treecolumn",
                        text: "名称",
                        dataIndex: "text"
                    }
                ]
            },
            listeners: {
                select: function ( me, record, index, eOpts) {
                    if (!record.raw.expanded) {
                          var store = Ext.data.StoreManager.lookup('kRreply');

                            store.load({
                                params: {
                                    id: record.raw.id  
                                },
                                callback: function (records, operation, success) {
                                   
                                }
                            })

                        Ext.getCmp("content").body.update(record.raw.Contents);
                        if (record.raw.FileOne != null && record.raw.FileOne != "") {
                            Ext.getCmp("f1").setVisible(true);
                            var f1 = record.raw.FileOne.split("_");
                            Ext.getCmp("f1").setText(f1[1]);
                        }
                        else {
                            Ext.getCmp("f1").setText("");
                            Ext.getCmp("f1").setVisible(false);
                        }

                        if (record.raw.FileTwo != null && record.raw.FileTwo != "") {
                            Ext.getCmp("f2").setVisible(true);
                            var f2 = record.raw.FileTwo.split("_");
                            Ext.getCmp("f2").setText(f2[1]);
                        }
                        else {
                            Ext.getCmp("f2").setText("");
                            Ext.getCmp("f2").setVisible(false);
                        }
                    }
                    else {
                        Ext.getCmp("f2").setText("");
                        Ext.getCmp("f2").setVisible(false);
                        Ext.getCmp("f1").setText("");
                        Ext.getCmp("f1").setVisible(false);
                        Ext.getCmp("content").body.update("");
                        store = Ext.data.StoreManager.lookup('kRreply');
                        store.loadData({});
                    }
                }
            }
        });
     this.orgTree = orgTree;
     return orgTree;
    },
   
    getReplyItem: function () {
        me = this;
        var emodel = Ext.define("moldes", {
            extend: "Ext.data.Model",
            fields: ["id", "KthemeID", "Contents", "CreatTime", "CreatPerson"]
        })
        var store = Ext.create("Ext.data.Store", {
            model: emodel,
            storeId: 'kRreply',
            proxy: {
                type: 'ajax',
                url: '/Knowledge/GetKreplyItem',
                extraParams: {},
                actionMethods: { read: 'POST' },
                reader: {
                    type: 'json',
                    root: 'Data'
                }
            }
        })
        var resultTpl = Ext.create('Ext.XTemplate',
           '<tpl for=".">',
           '<div class="search-item">',
               '<h3><span>{CreatTime:this.formatDate}</span>',
               '<a>{CreatPerson}</a></h3>',
               '<p>{Contents}</p>',
           '</div></tpl>',
           {
               formatDate: function (value) {
                   var dt = eval("new " + value.substr(1, value.length - 2)).toString();
                   return Ext.util.Format.date(dt, "Y-m-d H:i");
           }
          });
        return Ext.create("Ext.view.View", {
            viewConfig: { loadingText: "数据加载中...." },
            overflowY: 'auto',
            tpl: resultTpl,
            store: store,
            itemSelector: 'div.search-item',
            emptyText: '<div class="x-grid-empty">没有回复信息</div>'
          
        })
    },

    replywindow: function (id) {
        me = this;
        var cc= Ext.create("Ext.window.Window", {
            modal: true,
            id: 'arcform',
            title: '发表回复',
            width: 700,
            height: 230,
            layout: "fit",
            items: [
                 {
                     xtype: "form",
                     id: "editform",
                     region: "south",
                     border:0,
                     layout: 'fit',
                     items: [{
                         xtype: 'hidden',
                         name: 'KthemeID',
                         id: 'kthemeid',
                         value:id
                     },
                         {
                             xtype: 'htmleditor',
                             id: "contents",
                             name: "contens",
                             value: ""
                         }
                     ],
                     buttonAlign: 'left',
                     buttons: [
                         {
                             text: "回复",
                             handler: this.addreply,
                             formBind: true

                         }
                     ]
                 }
            ]
        }).show();
     me.replywin = cc
     Ext.getCmp("contents").setValue("");

     return cc;
    },

    addreply: function () {
        me = this;
        var content = Ext.getCmp("contents");
        if (content.getValue() == "")
        {
            PSI.MsgBox.showInfo("回复内容不允许为空！");
            return;
        }
        var f = Ext.getCmp("editform");
        var el = f.getEl() || Ext.getEl();
        el.mask();
        f.submit({
            url: "/Knowledge/AddKreply",
            method: "POST",
            success: function (form, action) {
                el.unmask();
                PSI.MsgBox.showInfo("回复完成！");
                Ext.getCmp("arcform").close();
                var tree = Ext.getCmp("tree");
                var item = tree.getSelectionModel().getSelection();
               
                stroe = Ext.data.StoreManager.lookup('zzjg');
                var nh = stroe.getRootNode();
                rocad = stroe.getNodeById(item[0].raw.id);
                tree.getSelectionModel().clearSelections();
                tree.getSelectionModel().select(rocad);
            },
            failure: function (form, action) {
                el.unmask();
                PSI.MsgBox.showInfo("服务器或网络问题,回复失败！。");
            }
        });
    }

})